using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

//用户注册
namespace ApiProject.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class RegisterController : ControllerBase
    {
        private IRepository<Users> _usersRepository;

        private IRepository<UserInfo> _userInfoRepository;
        private IRepository<Roles> _roleInfoRepository;

        public RegisterController(
            IRepository<Users> usersRepository,
            IRepository<UserInfo> userInfoRepository,
            IRepository<Roles> roleInfoRepository
        )
        {
            _usersRepository = usersRepository;

            _userInfoRepository = userInfoRepository;
            _roleInfoRepository = roleInfoRepository;
        }

        //用户注册
        //Post: /register
        [HttpPost]
        public dynamic Post(NewUser model)
        {
            var username = model.Username.Trim();
            var password = model.Password.Trim();
            var checkpassword = model.CheckPassword.Trim();

            var role = _roleInfoRepository.Table.Where(x => x.RoleName == "普通用户").FirstOrDefault();


            // 判断传入的username是否为空
            if (string.IsNullOrEmpty(username))
            {
                var temp =
                    new
                    {
                        Code = 1000,
                        Data = "",
                        Msg = "用户名不能为空，请输入用户名"
                    };
                return JsonHelper.Serialize(temp);
            }
            else
            {
                var users = _usersRepository.Table.Where(x => x.Username == username).FirstOrDefault();
                //判断是否存在该用户
                if (users != null)
                {
                    if (users.IsDelete == true)
                    {
                        //判断两次密码是否相同
                        if (checkpassword == password)
                        {
                            var user =
                                new Users
                                {
                                    Username = username,
                                    Password = password,
                                    RolesId = role.Id
                                };
                            _usersRepository.Insert(user);
                            _usersRepository.Table.Where(x => x.Username == username).FirstOrDefault();
                            var userInfro = new UserInfo
                            {
                                UserId = user.Id
                            };

                            _userInfoRepository.Insert(userInfro);

                            return JsonHelper
                                .Serialize(new
                                {
                                    Code = 200,
                                    Data = user,
                                    Msg = "创建用户成功"
                                });
                        }
                        else
                        {
                            return JsonHelper
                                .Serialize(new
                                {
                                    Code = 1000,
                                    Data = "",
                                    Msg = "两次密码不同请重新输入"
                                });
                        }
                    }
                    else
                    {
                        return JsonHelper.Serialize(new
                        {
                            Code = 1000,
                            Data = "",
                            Msg = "用户名已存在，请重新输入用户名"
                        });
                    }
                }
                else
                {
                    //判断两次密码是否相同
                    if (checkpassword == password)
                    {
                        var user =
                            new Users
                            {
                                Username = username,
                                Password = password,
                                RolesId = role.Id
                            };
                        _usersRepository.Insert(user);
                        _usersRepository.Table.Where(x => x.Username == username).FirstOrDefault();
                        var userInfro = new UserInfo
                        {
                            UserId = user.Id
                        };

                        _userInfoRepository.Insert(userInfro);

                        return JsonHelper
                            .Serialize(new
                            {
                                Code = 200,
                                Data = user,
                                Msg = "创建用户成功"
                            });
                    }
                    else
                    {
                        return JsonHelper
                            .Serialize(new
                            {
                                Code = 1000,
                                Data = "",
                                Msg = "两次密码不同请重新输入"
                            });
                    }
                }
            }
        }


        //店铺注册
        // Post /register/stor
        [HttpPost, Route("stor")]
        public dynamic StorRegister(NewUser model)
        {

            var username = model.Username.Trim();
            var password = model.Password.Trim();
            var checkpassword = model.CheckPassword.Trim();
            var nickname = model.NickName.Trim();

            var role = _roleInfoRepository.Table.Where(x => x.RoleName == "店铺").FirstOrDefault();

            // 判断传入的username是否为空
            if (string.IsNullOrEmpty(username))
            {
                var temp =
                    new
                    {
                        Code = 1000,
                        Data = "",
                        Msg = "店铺账号不能为空，请输入店铺账号"
                    };
                return JsonHelper.Serialize(temp);
            }
            else
            {
                var users = _usersRepository.Table.Where(x => x.Username == username).FirstOrDefault();
                //判断是否存在该用户
                if (users != null)
                {
                    if (users.IsDelete == true)
                    {
                        //判断两次密码是否相同
                        if (checkpassword == password)
                        {
                            var user =
                                new Users
                                {
                                    Username = username,
                                    Password = password,
                                    RolesId = role.Id
                                };
                            _usersRepository.Insert(user);
                            _usersRepository.Table.Where(x => x.Username == username).FirstOrDefault();
                            var userInfro = new UserInfo
                            {
                                UserId = user.Id,
                                NickName = nickname
                            };

                            _userInfoRepository.Insert(userInfro);

                            return JsonHelper
                                .Serialize(new
                                {
                                    Code = 200,
                                    Data = user,
                                    Msg = "创建店铺成功"
                                });
                        }
                        else
                        {
                            return JsonHelper
                                .Serialize(new
                                {
                                    Code = 1000,
                                    Data = "",
                                    Msg = "两次密码不同请重新输入"
                                });
                        }
                    }
                    else
                    {
                        return JsonHelper.Serialize(new
                        {
                            Code = 1000,
                            Data = "",
                            Msg = "用户名已存在，请重新输入用户名"
                        });
                    }
                }
                else
                {
                    //判断两次密码是否相同
                    if (checkpassword == password)
                    {
                        var user =
                            new Users
                            {
                                Username = username,
                                Password = password,
                                RolesId = role.Id
                            };
                        _usersRepository.Insert(user);
                        _usersRepository.Table.Where(x => x.Username == username).FirstOrDefault();
                        var userInfro = new UserInfo
                        {
                            UserId = user.Id,
                            NickName = nickname
                        };

                        _userInfoRepository.Insert(userInfro);

                        return JsonHelper
                            .Serialize(new
                            {
                                Code = 200,
                                Data = user,
                                Msg = "创建用户成功"
                            });
                    }
                    else
                    {
                        return JsonHelper
                            .Serialize(new
                            {
                                Code = 1000,
                                Data = "",
                                Msg = "两次密码不同请重新输入"
                            });
                    }
                }
            }

        }

        //     [HttpPost, Route("role")]
        //     public dynamic addRole(NewRole model)
        //     {
        //         var rolename = model.RoleName.Trim();
        //         var role = new Roles
        //         {
        //             RoleName = rolename,
        //         };
        //         _roleInfoRepository.Insert(role);

        //         return JsonHelper
        //                         .Serialize(new
        //                         {
        //                             Code = 200,
        //                             Data = role,
        //                             Msg = "成功"
        //                         });

        //     }
    }
}
